from django.db import models

from home.models import Banner, Nav
from utils.basemodel import BaseModel
from ckeditor.fields import RichTextField


class Category(BaseModel):
    name = models.CharField(max_length=50, verbose_name='名称')
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        verbose_name='父类',
        related_name="children"
    )
    intro = models.CharField(max_length=200)


    class Meta:
        db_table = "fgcategory"
        verbose_name = "分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Teacher(BaseModel):
    name = models.CharField(max_length=50,default="", verbose_name='教师名称')
    worker = models.CharField(max_length=50,default="", verbose_name='职称')
    intro = models.CharField(max_length=200,default="", verbose_name='简介')
    headimg = models.CharField(max_length=200,default="", verbose_name='头像')

    class Meta:
        db_table = "fgteacher"
        verbose_name = "教师"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Course(BaseModel):
    name = models.CharField(max_length=50,default="", verbose_name='课程名称')
    level = models.CharField(max_length=50,choices=(("0","零基础"),("1","初级"),("2","进阶"),("3","实战")),default="", verbose_name='课程等级')
    students = models.IntegerField(default=0, verbose_name='学习人数')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    discount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='优惠价格')
    nav = models.ForeignKey(
        Nav,
        on_delete=models.CASCADE,
        verbose_name='导航'
    )
    category = models.ForeignKey(
        Category,
        on_delete=models.CASCADE,
        verbose_name='分类'
    )
    teacher = models.ForeignKey(
        Teacher,
        on_delete=models.CASCADE,
        verbose_name='教师'
    )
    intro = models.CharField(max_length=200,default="", verbose_name='课程简介')
    detail = RichTextField()
    timeline = models.CharField(max_length=200,default="", verbose_name='课程时间')
    isend = models.BooleanField(default=False, verbose_name='是否已结束')
    cover = models.CharField(max_length=200,default="", verbose_name='课程封面')
    vedio = models.CharField(max_length=200,default="", verbose_name='课程视频')
    class Meta:
        db_table = "fgcourse"
        verbose_name = "课程"
        verbose_name_plural = verbose_name

class Tags(BaseModel):
    name = models.CharField(max_length=50,default="", verbose_name='标签名称')
    class Meta:
        db_table = "fgtags"
        verbose_name = "标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class CourseTags(models.Model):
    course = models.ForeignKey(
        Course,
        on_delete=models.CASCADE,
        verbose_name='课程',
        related_name='tags'

    )
    tags = models.ForeignKey(
        Tags,
        on_delete=models.CASCADE,
        verbose_name='标签'
    )

    class Meta:
        db_table = "fgcoursetags"
        verbose_name = "课程标签"


class Chapter(BaseModel):
    name = models.CharField(max_length=100,default="", verbose_name='章节名称')
    order = models.IntegerField(default=0, verbose_name='章节顺序')
    course = models.ForeignKey(
        Course,
        on_delete=models.CASCADE,
        verbose_name='课程',
        related_name='chapters'
    )
    timeline = models.IntegerField(verbose_name='时长')
    class Meta:
        db_table = "fgchapter"

    def __str__(self):
        return self.name


class Lession(BaseModel):
    name = models.CharField(max_length=100,default="", verbose_name='课时名称')
    order = models.IntegerField(default=0, verbose_name='课时顺序')
    time = models.IntegerField(verbose_name='时长',default=0)
    url = models.CharField(max_length=200,default="", verbose_name='视频链接')
    chapter = models.ForeignKey(
        Chapter,
        on_delete=models.CASCADE,
        verbose_name='章节',
        related_name='lessions'
    )

    class Meta:
        db_table = "fglession"

    def __str__(self):
        return self.name












